package com.hjy.hjyojbackendjudgeservice.judge.rabbitMq;

import com.hjy.hjyojbackendcommon.constant.OjMqConstant;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * @version 1.0
 * @Author HJY_Maker
 * @Date 2024/7/8 星期一 21:35
 */
@Slf4j
public class InitRabbitMq {
    public static void initMq(){
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            String EXCHANGE_NAME = OjMqConstant.OJ_EXCHANGE_NAME;
            //声明交换机类型
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");
            // 创建队列，随机分配一个队列名称
            String queueName = OjMqConstant.OJ_QUEUE_NAME;
            channel.queueDeclare(queueName, true, false, false, null);
            channel.queueBind(queueName, EXCHANGE_NAME, OjMqConstant.OJ_ROUTING_KEY);
            log.info("消息队列启动成功！");
        } catch (Exception e) {
            log.error("消息队列启动失败"+e.getMessage());
        }
    }
    public static void main(String[] args) {
        initMq();
    }
}
